﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Thv.admin.Pages
{
    public partial class thongke : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int homnay = DateTime.Today.Day;
                try
                {
                    if (homnay < 16)
                    {
                        txtFrom.Date = DateTime.Parse(DateTime.Now.Month + "/01/" + DateTime.Now.Year);
                        txtTo.Date = DateTime.Parse(DateTime.Now.Month + "/15/" + DateTime.Now.Year);
                    }
                    else
                    {
                        txtFrom.Date = DateTime.Parse(DateTime.Now.Month + "/16/" + DateTime.Now.Year);
                        txtTo.Date = DateTime.Now;

                    }
                }
                catch (Exception) { }
                Bindata();
                BinMamoi_macu();
            }
        }

        protected void btnXlsxExport1_Click(object sender, EventArgs e)
        {
           grvExpord.WriteXlsxToResponse();
        }

        void BinMamoi_macu()
        {
            // select * from uf_thongketoanhethong (14,'09/01/2012','10/30/2012','11/01/2012','11/30/2012')
            try
            {
                string ngaycuoi = "30";
                string thangdau = "1";
                string thang = "1";
                int ncuoi = DateTime.Today.Day;
                int thangnay = DateTime.Today.Month;
                int thangtruoc = thangnay - 1;
                //kiem tra thang nay neu la 1 thi xet thang truoc la 12
                if (thangnay == 1)
                    thangtruoc = 12;
                // neu thang truoc ma nho hon 10 thi cong them so khong nguoc lai thi = thang truoc 
                if (thangtruoc < 10)
                    thangdau = "0" + thangtruoc;
                else
                    thangdau = thangtruoc.ToString();
                // neu ngay cuoi thang ma nho hon 10 thi cộng them so 0
                if (ncuoi < 10)
                    ngaycuoi = "0" + ncuoi;
                //neu thang truoc ma bang 2 thi xet ngay cuoi cung cua thang do =28
                if (thangtruoc == 2)
                    ngaycuoi = "28";
                else
                    ngaycuoi = "30";
                // neu thang nay ma nho hon 10 thi + them 0
                if (thangnay < 10)
                    thang = "0" + thangnay;
                else
                    thang = thangnay.ToString();


                int nam = DateTime.Now.Year;
                if (thangnay == 1)
                    nam = nam - 1;

                string dteForm = DateTime.Parse("01/10/2012").ToString("MM/dd/yyyy");
                string dteTo = thangdau + "/" + ngaycuoi + "/" + nam;

                string dteForm2 = thang + "/" + "01" + "/" + DateTime.Now.Year;
                string dteTo2 = DateTime.Now.ToString("MM/dd/yyyy");

                if (txtFrom.Text != "")
                    dteForm2 = txtFrom.Date.ToString("MM/dd/yyyy");
                if (txtTo.Text != "")
                    dteTo2 = txtTo.Date.ToString("MM/dd/yyyy");

                string Sql = string.Format("select * from  uf_ThongkeMamoi_Macu({0},{1},{2},{3})", "'" + dteForm + "'", "'" + dteTo + "'", "'" + dteForm2 + "'", "'" + dteTo2 + "'");
                Xuly xl = new Xuly();
                grvTV.DataSource = xl.ExcuteSQL_Table(Sql).DefaultView;
                grvTV.DataBind();
            }
            catch (Exception) { }
        }

        void Bindata()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var showlist = from k in dx.tbTemps
                           select k;
            //Xuly xl = new Xuly();
            //DataTable  dt = xl.ExcuteSQL_Table("select * from 
            grvTV0.DataSource = showlist;
            grvTV0.DataBind();
        }

        void Binbangtemp()
        {
           // select * from uf_thongketoanhethong (14,'09/01/2012','10/30/2012','11/01/2012','11/30/2012')
            try
            {
                string ngaycuoi = "30";
                string thangdau = "1";
                string thang = "1";
                int ncuoi = DateTime.Today.Day;
                int thangnay = DateTime.Today.Month;
                int thangtruoc = thangnay - 1;
                //kiem tra thang nay neu la 1 thi xet thang truoc la 12
                if (thangnay == 1)
                    thangtruoc = 12;
                // neu thang truoc ma nho hon 10 thi cong them so khong nguoc lai thi = thang truoc 
                if (thangtruoc < 10)
                    thangdau = "0" + thangtruoc;
                else
                    thangdau = thangtruoc.ToString();
                // neu ngay cuoi thang ma nho hon 10 thi cộng them so 0
                if (ncuoi < 10)
                    ngaycuoi = "0" + ncuoi;
                //neu thang truoc ma bang 2 thi xet ngay cuoi cung cua thang do =28
                if (thangtruoc == 2)
                    ngaycuoi = "28";
                else
                    ngaycuoi = "30";
                // neu thang nay ma nho hon 10 thi + them 0
                if (thangnay < 10)
                    thang = "0" + thangnay;
                else
                    thang = thangnay.ToString();


                int nam = DateTime.Now.Year;
                if (thangnay == 1)
                    nam = nam - 1;

                string dteForm = DateTime.Parse("01/10/2012").ToString("MM/dd/yyyy");
                string dteTo = thangdau + "/" + ngaycuoi + "/" + nam;

                string dteForm2 = thang + "/" + "01" + "/" + DateTime.Now.Year;
                string dteTo2 = DateTime.Now.ToString("MM/dd/yyyy");

                if (txtFrom.Text != "")
                    dteForm2 = txtFrom.Date.ToString("MM/dd/yyyy");
                if (txtTo.Text != "")
                    dteTo2 = txtTo.Date.ToString("MM/dd/yyyy");

                string Sql = string.Format("select * from  uf_thongketoanhethong({0},{1},{2},{3},{4})", 14, "'" + dteForm + "'", "'" + dteTo + "'", "'" + dteForm2 + "'", "'" + dteTo2 + "'");
                Xuly xl = new Xuly();
                grvTV0.DataSource = xl.ExcuteSQL_Table(Sql).DefaultView;
                grvTV0.DataBind();
            }
            catch (Exception) { }
        }


        protected void grvTV_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            BinMamoi_macu();
        }

        protected void grvTV0_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            Bindata();
        }

        protected void grvTV_Load(object sender, EventArgs e)
        {
            BinMamoi_macu();
        }

        protected void grvTV0_Load(object sender, EventArgs e)
        {
            Bindata();
        }

        protected void cbPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            switch (e.Parameter)
            {
                case "Luu":
                    InsertTemp();
                    Bindata();
                    break;
                case "Timkiem":
                    Binbangtemp();
                    BinMamoi_macu();
                    break;
            }
        }

        void InsertTemp()
        {
            // select * from uf_thongketoanhethong (14,'09/01/2012','10/30/2012','11/01/2012','11/30/2012')
            try
            {
                string ngaycuoi = "30";
                string thangdau = "1";
                string thang = "1";
                int ncuoi = DateTime.Today.Day;
                int thangnay = DateTime.Today.Month;
                int thangtruoc = thangnay - 1;
                //kiem tra thang nay neu la 1 thi xet thang truoc la 12
                if (thangnay == 1)
                    thangtruoc = 12;
                // neu thang truoc ma nho hon 10 thi cong them so khong nguoc lai thi = thang truoc 
                if (thangtruoc < 10)
                    thangdau = "0" + thangtruoc;
                else
                    thangdau = thangtruoc.ToString();
                // neu ngay cuoi thang ma nho hon 10 thi cộng them so 0
                if (ncuoi < 10)
                    ngaycuoi = "0" + ncuoi;
                //neu thang truoc ma bang 2 thi xet ngay cuoi cung cua thang do =28
                if (thangtruoc == 2)
                    ngaycuoi = "28";
                else
                    ngaycuoi = "30";
                // neu thang nay ma nho hon 10 thi + them 0
                if (thangnay < 10)
                    thang = "0" + thangnay;
                else
                    thang = thangnay.ToString();


                int nam = DateTime.Now.Year;
                if (thangnay == 1)
                    nam = nam - 1;

                string dteForm = "01/10/2012";
                string dteTo = thangdau + "/"+ ngaycuoi  + "/" + nam;

                string dteForm2 = thang + "/" + "01" + "/" + DateTime.Now.Year;
                string dteTo2 = DateTime.Now.ToString("dd/MM/yyyy");

                if (txtFrom.Text != "")
                    dteForm2 = txtFrom.Date.ToString();
                if (txtTo.Text != "")
                    dteTo2 = txtTo.Date.ToString();
                DateTime ngay1 = DateTime.Parse(dteForm);
                DateTime ngay2 = DateTime.Parse(dteTo);
                DateTime ngay3 = DateTime.Parse(dteForm2);
                DateTime ngay4 = DateTime.Parse(dteTo2);

                DataClasses1DataContext dx = new DataClasses1DataContext();
                dx.sp_insertTemp(14, ngay1.ToString("MM/dd/yyyy"), ngay2.ToString("MM/dd/yyyy"), ngay3.ToString("MM/dd/yyyy"), ngay4.ToString("MM/dd/yyyy"));
                dx.SubmitChanges();
                
            }
            catch (Exception) { }
        }
    }
}